import sys
import pygame
from pygame.sprite import Group
from settings import Settings
from ship import Ship
from gamestats import Gamestats
from button import Button
from scoreboard import Scoreboard
import game_functions as gf 


def run_game():
    #初始化游戏并创建一个屏幕对象
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
    pygame.display.set_caption("Alien Invasion")

    #创建Play按钮
    play_button = Button(ai_settings,screen,"Play")

    #创建一个用于信息统计的实例
    stats = Gamestats(ai_settings)
    sb = Scoreboard(ai_settings,screen,stats)

    #创建一艘飞船
    ship = Ship(screen,ai_settings)

    #创建一个外星人编组
    aliens = Group()

    #创建外星人群
    gf.create_fleet(ai_settings,screen,ship,aliens)

    #创建一个用于存储子弹的编组
    bullets = Group()

    #开始游戏的主循环
    while True:

        #监视键盘和鼠标事件
        gf.check_events(ai_settings,stats,play_button,sb,screen,ship,aliens,bullets)

        if stats.game_active:
            #更新飞船位置信息
            ship.update()
            #更新子弹的位置
            gf.update_bullets(ai_settings,stats,sb,screen,ship,aliens,bullets)
            #更新外星人的位置
            gf.update_alien(ai_settings,stats,sb,screen,ship,aliens,bullets)
        
        #每次循环时都重新绘制屏幕
        gf.update_screen(ai_settings,stats,sb,screen,ship,aliens,bullets,play_button)

run_game()